package cn.xiehang.SoundSight;



import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.activity.EdgeToEdge;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainPage extends AppCompatActivity {
    private Button startRecordingButton;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.main_page_example);




        // 在你的 Activity 或 Fragment 中
        BottomNavigationView bottomNavigationView = findViewById(R.id.nav_view);

        bottomNavigationView.setItemIconTintList(null);  // 禁用图标着色
        switchFragment(FifthFragment.newInstance(), "FifthFragment"); // 默认加载第五个Fragment
        bottomNavigationView.setSelectedItemId(R.id.navigation_item5);  // 确保这个ID对应第五个Item
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Fragment fragment = null;
                String tag = null;

                int id = item.getItemId();
                if (id == R.id.navigation_item1) {
                    tag = "FirstFragment";
                    fragment = getSupportFragmentManager().findFragmentByTag(tag);
                    if (fragment == null) {
                        fragment = FirstFragment.newInstance(); // 使用静态工厂方法
                    }
                } else if (id == R.id.navigation_item2) {
                    tag = "SecondFragment";
                    fragment = getSupportFragmentManager().findFragmentByTag(tag);
                    if (fragment == null) {
                        fragment = SecondFragment.newInstance();
                    }
                } else if (id == R.id.navigation_item3) {
                    tag = "ThirdFragment";
                    fragment = getSupportFragmentManager().findFragmentByTag(tag);
                    if (fragment == null) {
                        fragment = ThirdFragment.newInstance();
                    }

                } else if (id == R.id.navigation_item4) {
                    tag = "FourthFragment";
                    fragment = getSupportFragmentManager().findFragmentByTag(tag);

                    if (fragment == null) {
                        fragment = FourthFragment.newInstance();
                    }
                }
                if (fragment != null) {
                    switchFragment(fragment, tag);
                    return true;
                }
                return false;
            }
        });

        startRecordingButton = findViewById(R.id.RecordingButton);

        startRecordingButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setEnabled(false); // 防止连续点击
                bottomNavigationView.setSelectedItemId(R.id.navigation_item5);
                switchFragment(FifthFragment.newInstance(), "FifthFragment");
                v.postDelayed(() -> v.setEnabled(true), 500); // 0.5秒后恢复点击
            }
        });

    }

    private void switchFragment(Fragment fragment, String tag) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();

        // 隐藏所有已添加的Fragment
            for (Fragment frag : fm.getFragments()) {
                if (frag != null && frag.isAdded()) {
                    transaction.hide(frag);
                }
            }



        // 检查是否已经添加过该Fragment
        Fragment existingFragment = fm.findFragmentByTag(tag);

        if (existingFragment != null) {
            // 从存储信息中更新
            existingFragment.onResume();
            // 如果已存在，则显示它
            transaction.show(existingFragment);
        } else {
            // 否则添加新的Fragment
            transaction.add(R.id.FragmentContainer, fragment, tag);

        }

        transaction.commit();
        fm.executePendingTransactions();
    }


//    public void loadFragment(Fragment fragment) {
//        // 创建 FragmentManager
//        FragmentManager fragmentManager = getSupportFragmentManager();
//        // 创建 FragmentTransaction
//        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//        // 替换 Fragment
//        fragmentTransaction.replace(R.id.FragmentContainer, fragment);
//        fragmentTransaction.commit();
//    }

}


